// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

var ClickManager = require('ClickManager')
var AudioManager = require('AudioManager')
var NetworkManager = require('NetworkManager')

cc.Class({
    extends: cc.Component,

    properties: {
        isUnread: 0,
        id: 0,
        createTime: cc.Label,
        msg: cc.Label,
        unreadLabel: cc.Label,

        deletedAnimation: cc.AnimationClip,

        deleteNode: cc.Node,
        bigNode: cc.Node
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        var animation = this.node.addComponent(cc.Animation)
        animation.addClip(this.deletedAnimation, 'deleted')

        // 删除节点
        ClickManager.onClick(this.deleteNode, this.deleteNode, () => {
            animation.play('deleted')
            AudioManager.loadAndPlaySound(this.node, 'audio/deleted.mp3')

            animation.on('stop', () => {
                this.bigNode.removeAllChildren()
                this.bigNode.destroy()

                NetworkManager.doEmailDeleted(this.id)
            }, this)
        })

        // 已读信息了
        ClickManager.onClick(this.bigNode, this.bigNode, () => {
            this.unreadLabel.node.color = new cc.Color(0, 153, 102)
            this.unreadLabel.string = '- 已读邮件'

            NetworkManager.doEmailRead(this.id)
        })
    },

    start () {

    },

    // 初始化数据
    init (data) {
        this.isUnread = data.isUnread
        this.id = data.id
        this.createTime.string = data.createTime
        this.msg.string = data.msg

        if (data.isUnread === 1) {
            this.unreadLabel.node.color = new cc.Color(153, 15, 0)
            this.unreadLabel.string = '- 未读邮件'
        } else {
            this.unreadLabel.node.color = new cc.Color(0, 153, 102)
            this.unreadLabel.string = '- 已读邮件'
        }
    }

    // update (dt) {},
});
